package cn.edu.tju.ldj.analytics.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.tju.ldj.analytics.Factory;
import cn.edu.tju.ldj.analytics.alphas.IAlphaEngine;
import cn.edu.tju.ldj.analytics.vo.Broker;
import cn.edu.tju.ldj.analytics.vo.Contract;
import cn.edu.tju.ldj.analytics.vo.Good;
import cn.edu.tju.ldj.analytics.vo.components.BtnAlpha;

/**
 * 其他人无需了解,为报表服务的类
 * 
 * @author cjy
 * 
 */
public class IServiceGood {

	private List<Broker> brokerLst;
	private Map<String, Good> allGoods;
	
	public IServiceGood() throws Exception {
		brokerLst = Factory.dataProcessor.loadSrcData();
		
		allGoods = new HashMap<String, Good>();
		for (int i = 0; brokerLst != null && i < brokerLst.size(); i++) {
			for (int j = 0; j < brokerLst.get(i).getBrokerGoodLst().size(); j++) {
				allGoods.put(brokerLst.get(i).getBrokerGoodLst().get(j).getId(), brokerLst.get(i).getBrokerGoodLst().get(j));
			}
		}
	}

	public void reload() throws Exception {
		brokerLst = Factory.dataProcessor.loadSrcData();
	}

	public List<Broker> getBrokersByType(String goodType) {
		List<Broker> matchLst = new ArrayList<Broker>();
		for (int i = 0; i < brokerLst.size(); i++) {
			if(brokerLst.get(i).getGoodType().equals(goodType)){
				matchLst.add(brokerLst.get(i));
			}
		}
		return matchLst;
	}
	
	public IAlphaEngine getAlphaEngine(BtnAlpha alpha) throws Exception {

		Factory.utilSys.ASSERT(alpha != null);
		return (IAlphaEngine) Factory.utilReflect.loadInstance(alpha
				.getAlphaMethodClass());
	}

	/**
	 * 刘大健用，重新载入一个报表
	 * 
	 * @param id
	 * @throws Exception
	 */
	public Good reload(String id) throws Exception {
		List<Broker> temp = Factory.dataProcessor.loadSrcData();
		for (int i = 0; i < temp.size(); i++) {
			for (int j = 0; j < temp.get(i).getBrokerGoodLst().size(); j++) {
				if (temp.get(i).getBrokerGoodLst().get(j).getId().equals(id)) {
					allGoods.put(temp.get(i).getBrokerGoodLst().get(j).getId(), temp.get(i).getBrokerGoodLst().get(j));
					brokerLst.set(i, temp.get(i));
					return temp.get(i).getBrokerGoodLst().get(j);
				}
			}
		}

		throw new java.lang.Exception("Invalid good id:" + id);

	}

	/**
	 * 通过一个报表id取得报表实例
	 * 
	 * @param id
	 * @return
	 */
	public Good getGoodById(String id) {
		
		if (allGoods.containsKey(id)) {
			return allGoods.get(id);
		} else {
			throw new java.lang.IllegalArgumentException("invalid good id:"
					+ id);
		}

	}

	public List<Contract> getContractsByGoodId(String goodId) throws Exception {
		// TODO Auto-generated method stub
		Good temGood = getGoodById(goodId);
		Factory.dataProcessor.loadContractsForGood(temGood);
		return temGood.getGoodContractLst();
	}

	public String getContractDetailById(String ctrctid) throws Exception {
		// TODO Auto-generated method stub
		Contract ct = null;
		for (String key: allGoods.keySet()) {
			if(allGoods.get(key).getGoodContracts().containsKey(ctrctid)){
				ct = allGoods.get(key).getGoodContracts().get(ctrctid);
			}
		}
		if(ct == null){
			return null;
		}else{
			IAlphaEngine alpahEngine = getAlphaEngine(Factory.utilParam.getCurAlpha());
			if(ct.getCtrctDayDataLst().size() > 0){
				if(!ct.getCtrctDayDataLst().get(0).containsKey("agv1")){
					Factory.dataProcessor.getAvgs(ct);
				}
			}
			alpahEngine.processSimulation(ct, true);
			return Factory.dataProcessor.initContractInfoJson(ct);
		}
		
	}

}
